﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using netDxf;
using netDxf.Entities;

using System.Globalization;
using Microsoft.Win32;

using FDXF;

namespace FDXF
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }



        public int ViewFDXF(string  filename)
        {

            DxfDocument dxf = new DxfDocument();  //定义dxf文件
            DxfDocument dxfLoad = DxfDocument.Load(filename);  //加载DXF文件

            int line_count = dxfLoad.Lines.Count();
            return line_count;

        }

        private void Button1_Click(object sender, EventArgs e)
        {

            OpenFileDialog openDlg = new OpenFileDialog();
            openDlg.Filter = "DXF 文件|*.dxf";

            //string filename = "HG_20592_BL-RF.dxf";
            string filename = "HG_20592_BL-RF_第一第二.dxf";
            //string filename = "HG_20592_BL-RF_第一第二第三.dxf";

            DxfDocument dxf = new DxfDocument();
            DxfDocument dxfLoad = DxfDocument.Load(filename);
            ArrayList lineListX = new ArrayList();
          

            int line_count = dxfLoad.Lines.Count(); //加载DXF文件
            //double[,] linePointStart = new double[line_count, line_count];//定义并初始化二维数组
            //List<List<float>> linePointStart = new List<List<float>>(); //使用泛型list<T>，定义直线起点的二维数组
            List<float> linePointStartX = new List<float>(); //定义直线起点的X值的数组
            List<float> linePointStartY = new List<float>(); //定义直线起点的Y值的数组

            //List<List<float>> linePointEnd = new List<List<float>>(); //使用泛型list<T>，定义直线终点的二维数组
            List<float> linePointEndX = new List<float>(); //定义直线终点的X值的数组
            List<float> linePointEndY = new List<float>(); //定义直线终点的X值的数组


            foreach (netDxf.Entities.Line ln in dxfLoad.Lines) //遍历DXF文件中的直线
            {
                linePointStartX.Add(Convert.ToSingle(ln.StartPoint.X));  //直线起点的X值，加入数组,同时数据类型转为float
                linePointStartY.Add(Convert.ToSingle(ln.StartPoint.Y));  //直线起点的Y值，加入数组,同时数据类型转为float
                linePointEndX.Add(Convert.ToSingle(ln.EndPoint.X));    //直线终点的X值，加入数组,同时数据类型转为float
                linePointEndY.Add(Convert.ToSingle(ln.EndPoint.Y));   //直线终点的X值，加入数组,同时数据类型转为float
            }

            List<float> ExLineX = new List<float>(); //定义直线的最大和最小X值的数组
            ExLineX.Add(linePointStartX.Max());
            ExLineX.Add(linePointEndX.Max());
            ExLineX.Add(linePointStartX.Min());
            ExLineX.Add(linePointEndX.Min());

            List<float> ExLineY = new List<float>(); //定义直线的最大和最小Y值的数组
            ExLineY.Add(linePointStartY.Max());
            ExLineY.Add(linePointEndY.Max());
            ExLineY.Add(linePointStartY.Min());
            ExLineY.Add(linePointEndY.Min());


            //以下为计算图形X方向移动到原点的距离
            //通过移动图形使图形的左下角和原点重合

            float DrawingWidth = Math.Abs(ExLineX.Max() - ExLineX.Min()); //需显示图形的宽度
            float DrawingHeight = Math.Abs(ExLineY.Max() - ExLineY.Min()); //需显示图形的高度
            float FormWidth = pictureBox1.Width; //窗体的宽度
            float FormHeight = pictureBox1.Height; //窗体的高度

            float MoveX;         
            
            if (ExLineX.Min()<=0) //如果直线X轴的最小值为负或零时
            {
             MoveX = Math.Abs(ExLineX.Min()); //移动的数值

            }
            else
            {
             MoveX = -ExLineX.Min(); //移动的数值

            }

            //以下为计算图形Y方向移动到原点的距离
            //通过移动图形使图形的左下角和原点重合

            float MoveY;
            if (ExLineY.Min() <= 0) //如果直线X轴的最小值为负或零时
            {
                //MoveY = Math.Abs(ExLineY.Min())- Math.Abs(DrawingHeight / 2); //移动的数值
                MoveY = Math.Abs(ExLineY.Min()); //移动的数值

            }
            else
            {
                //MoveY = -ExLineY.Min()+ Math.Abs(DrawingHeight/2); //移动的数值
                MoveY = -ExLineY.Min(); //移动的数值
            }


            Graphics g = pictureBox1.CreateGraphics();  //创建画布
            Pen MyPen = new Pen(Color.White, 1);  //定义画笔的颜色和宽度

            float ZoomFactor; //换算因子
            float MoveRiginX; //坐标原点在+X轴上的移动系数

            MoveRiginX = Math.Abs(ExLineX.Min())/(Math.Abs(ExLineX.Min())+ Math.Abs(ExLineX.Max())); //计算坐标原点在+X轴上的移动系数

            g.TranslateTransform(0, pictureBox1.Height);  //移动原点,+5的目的是为了，使图形边缘离窗体有5的距离
            g.ScaleTransform(1f, -1f);//缩小或放大当前坐标系
                                      //g.TranslateTransform(MinX.Min(), MinY.Min());  //移动原点,+5的目的是为了，使图形边缘离窗体有5的距离
                                      //g.TranslateTransform(pictureBox1.Width* MoveRiginX, 1);  //移动原点,+5的目的是为了，使图形边缘离窗体有5的距离



            //以下为计算缩放比例
            float MoveCenterX; //因平移图形后，使得图形的左下角和窗体的左下角在同一点上，但图形按比例缩放后，图形不在窗体X轴的中心，
            float MoveCenterY; //因平移图形后，使得图形的左下角和窗体的左下角在同一点上，但图形按比例缩放后，图形不在窗体Y轴的中心，
                               //故设置此变量，用于计算图形距离窗体X和Y轴方向上中心的距离      

            if (pictureBox1.Width/ DrawingWidth > pictureBox1.Height/ DrawingHeight) //比较 长度比和宽度比的比值(窗体和图形)
            {

                ZoomFactor = ((pictureBox1.Height) / DrawingHeight)*0.95f; //计算窗体高度和图中的图形的高度的比值,EdgeDistX
                MoveCenterX = Math.Abs(DrawingWidth * ZoomFactor - FormWidth) / 2;
                MoveCenterY = Math.Abs(DrawingHeight * ZoomFactor - FormHeight) / 2;
            }
            else
            {
                ZoomFactor = ((pictureBox1.Width) / DrawingWidth)*0.95f;  //计算窗体宽度和图中的图形的宽度的比值,EdgeDistY
                MoveCenterX = Math.Abs(DrawingWidth * ZoomFactor - FormWidth) / 2;
                MoveCenterY = Math.Abs(DrawingHeight * ZoomFactor - FormHeight) / 2;
                
            }


            //在画布中画出图形，且在窗体中心显示
            for (int i = 0; i < line_count ; i = i + 1)
            {
                g.DrawLine(MyPen, (linePointStartX[i]+ MoveX) * ZoomFactor+ MoveCenterX, (linePointStartY[i]+ MoveY) * ZoomFactor+ MoveCenterY, (linePointEndX[i]+ MoveX) * ZoomFactor+ MoveCenterX, (linePointEndY[i]+ MoveY) * ZoomFactor+ MoveCenterY);//在画板上画直线,起始坐标为(X1,Y1),终点坐标为(X2,Y2)
                //linePointStartX-----DXF文件中的原始坐标
                //MoveX，MoveY--------通过整体移动图形，使图形的坐下角和窗体的左下角重合，即原点重合
                //ZoomFactor----------缩放系数
                //MoveCenterX，Y------用于计算图形距离窗体X和Y轴方向上中心的距离             
            }

            //richTextBox1.Text = DrawingWidth.ToString() +"; "+ DrawingHeight.ToString() + "\n" + pictureBox1.Width.ToString() + "; " + pictureBox1.Height.ToString() + "\n" + ZoomFactor.ToString() + "\n" + ExLineX.Min() + "; " + ExLineY.Min();

            //MessageBox.Show(MoveCenterY.ToString());

            int arc_count = dxfLoad.Arcs.Count(); //加载DXF文件
            MessageBox.Show(arc_count.ToString());



        }

        private void button2_Click(object sender, EventArgs e)
        {
            string  filename = "HG_20592_BL-RF_第一第二.dxf";
            LoadEntities line = new LoadEntities();//实例化类   

            line.GetFilename(filename);//调用类中的方法
            line.Getline();//调用类中的方法
            int x = line.line_count;

            Graphics g = pictureBox1.CreateGraphics();  //创建画布
            Pen MyPen = new Pen(Color.White, 1);  //定义画笔的颜色和宽度

            g.TranslateTransform(0, pictureBox1.Height); //移动原点
            g.ScaleTransform(1f, -1f);//缩小或放大当前坐标系

            for (int i = 0; i < x; i = i + 1)
            {
                g.DrawLine(MyPen, line.linePointStartX[i], line.linePointStartY[i], line.linePointEndX[i] , line.linePointEndY[i]);//在画板上画直线,起始坐标为(X1,Y1),终点坐标为(X2,Y2)
           
            }

        }

        private void button3_Click(object sender, EventArgs e)
        {
            string filename = "lee.dxf";
            //string filename = "leeT.dxf";

            DrawingDxf line = new DrawingDxf();//实例化类   


            line.Draw(pictureBox1, filename);//调用类中的方法    

            
        }
    }
}
